﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Workbench.Views;
using SharedGenomics.Core;

namespace SharedGenomics.Workbench.Presenters
{
    public class ValidationErrorListPresenter : Presenter
    {
        private ValidationMessageCollection _validationMessages;
        private bool _showErrors;
        private bool _showWarnings;

        public ValidationErrorListPresenter(IValidationErrorListView view)
            : base(view)
        {
            view.ShowErrorsClicked += new EventHandler(this.View_ShowErrorsClicked);
            view.ShowWarningsClicked += new EventHandler(this.View_ShowWarningsClicked);

            this.ValidationView = view;
            this.ShowErrors = true;
            this.ShowWarnings = true;
        }

        private void View_ShowErrorsClicked(object sender, EventArgs e)
        {
            this.ShowErrors = this.ValidationView.IsShowErrorsSelected;
        }

        private void View_ShowWarningsClicked(object sender, EventArgs e)
        {
            this.ShowWarnings = this.ValidationView.IsShowWarningsSelected;
        }

        private void SetViewMessages()
        {
            IEnumerable<ValidationMessage> messages = Enumerable.Empty<ValidationMessage>();
            if (this._validationMessages != null)
            {
                if (this.ShowErrors)
                    messages = messages.Concat(this._validationMessages.ErrorMessages);

                if (this.ShowWarnings)
                    messages = messages.Concat(this._validationMessages.WarningMessages);
            }

            this.ValidationView.Messages = messages;
        }

        public ValidationMessageCollection ValidationMessages
        {
            set
            {
                this._validationMessages = value;
                this.SetViewMessages();
            }
        }

        private IValidationErrorListView ValidationView { get; set; }
        private bool ShowErrors
        {
            get { return this._showErrors; }
            set
            {
                bool changing = value ^ this._showErrors;
                this._showErrors = value;
                this.ValidationView.IsShowErrorsSelected = value;

                if (changing)
                {
                    this.SetViewMessages();
                }
            }
        }

        private bool ShowWarnings
        {
            get { return this._showWarnings; }
            set
            {
                bool changing = value ^ this._showWarnings;
                this._showWarnings = value;
                this.ValidationView.IsShowWarningsSelected = value;

                if (changing)
                {
                    this.SetViewMessages();
                }
            }
        }
    }
}
